package com.spyx.home.service.impl;

import com.atyx.spyx.vo.product.SkuInfoVo;
import com.spyx.activity.ActivityClient;
import com.spyx.home.service.ItemService;
import com.spyx.product.ProductClient;
import com.spyx.search.SkuFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductClient productClient;
    @Autowired
    private ActivityClient activityClient;
    @Autowired
    private SkuFeignClient skuFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map item(Long skuId, Long userId) {
        //1.封装结果
        Map mapresult=new HashMap();
        //2.创建CompletableFuture对象操作远程调用
        CompletableFuture<SkuInfoVo> task1 = CompletableFuture.supplyAsync(() -> {
            //1.调取service-product中的服务方法
            SkuInfoVo infoVo = productClient.getSkuInfoBySkuId(skuId);
            mapresult.put("skuInfoVo", infoVo);
            return infoVo;
        }, threadPoolExecutor);

        CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {
            //2.调取service-activity中的服务方法
            Map<String, Object> map = activityClient.findActivityAndCoupon(skuId, userId);
            mapresult.putAll(map);
        }, threadPoolExecutor);

        CompletableFuture<Void> task3 = CompletableFuture.runAsync(() -> {
            //3.调取service-search中的服务方法
            skuFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        CompletableFuture.allOf(task1,task2,task3).join();
        return mapresult;
    }
}
